package com.cloudkinto.controller.stock;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.dto.stock.flowing.FlowingPageDto;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.stock.ProductStockFlowingService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 * 库存流水 前端控制器
 * </p>
 * @author hua
 * @since 2024-11-28
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "库存流水WMS模块")
@RequestMapping("/api/admin/wms/ProductStockFlowing")
public class ProductStockFlowingWmsController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ProductStockFlowingWmsController.class);

    private final ProductStockFlowingService service;


    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "库存流水查询初始化", notes = "库存流水查询初始化")
    public SingleResult pageInit() {
        return service.pageInit(getCurrentUserId(), getCompanyId(), getTenantId(), "wms");
    }


    @UserSessionRequired
    @GetMapping("/productBook")
    @ApiOperation(value = "产品库存-库存流水查询（分页）", notes = "库存流水查询（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyIds", value = "客户 多个,隔开"),
            @ApiImplicitParam(name = "stockAttribute", value = "库存属性 0正品 1次品"),
            @ApiImplicitParam(name = "orderTypes", value = "单据类型 多个,隔开"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "noteNo", value = "关联单据号"),
            @ApiImplicitParam(name = "cellNo", value = "库位编码"),
            @ApiImplicitParam(name = "innerBatchNo", value = "批次号"),
            @ApiImplicitParam(name = "barcode", value = "barcode-模糊搜索"),
            @ApiImplicitParam(name = "barcodeAccurate", value = "barcode-精准搜索"),
            @ApiImplicitParam(name = "productName", value = "产品名称-模糊搜索"),
            @ApiImplicitParam(name = "skuAccurate", value = "sku-精准搜索"),
            @ApiImplicitParam(name = "spu", value = "spu-模糊搜索"),
            @ApiImplicitParam(name = "spuAccurate", value = "spu-精准搜索"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = FlowingPageDto.class))
    public PageResult productBook(HttpServletRequest request){

        Map<String, Object> map = this.getProductMap(request);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }


    public Map<String, Object> getProductMap(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String ids = request.getParameter("ids");
        if (ids != null) {
            map.put("ids", Arrays.asList(ids.split(",")));
        }
        map.put("flowingType", "product");
        map.put("tenantId", getTenantId());
        map.put("userId", getCurrentUserId());
        map.put("source", "wms");
        map.put("warehouseIds", Collections.singletonList(getWarehouseId()));

        String companyIds = request.getParameter("companyIds");
        if (StringUtils.isNotBlank(companyIds)) {
            map.put("companyIds", Arrays.asList(companyIds.split(",")));
        }
        String stockAttribute = request.getParameter("stockAttribute");
        map.put("stockAttribute", stockAttribute);

        String orderTypes = request.getParameter("orderTypes");
        if (StringUtils.isNotBlank(orderTypes)) {
            map.put("orderTypes", Arrays.asList(orderTypes.split(",")));
        }
        String beginTime = request.getParameter("beginTime");
        String endTime = request.getParameter("endTime");
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        String noteNo = request.getParameter("noteNo");
        String cellNo = request.getParameter("cellNo");
        String barcode = request.getParameter("barcode");
        String barcodeAccurate = request.getParameter("barcodeAccurate");
        String productName = request.getParameter("productName");
        String innerBatchNo = request.getParameter("innerBatchNo");
        map.put("innerBatchNo", innerBatchNo);
        map.put("noteNo", noteNo);
        map.put("cellNo", cellNo);
        map.put("barcode", barcode);
        map.put("barcodeAccurate", barcodeAccurate);
        map.put("productName", productName);

        String skuAccurate = request.getParameter("skuAccurate");
        map.put("skuAccurate", skuAccurate);

        String spuAccurate = request.getParameter("spuAccurate");
        String spu = request.getParameter("spu");
        map.put("spuAccurate", spuAccurate);
        map.put("spu", spu);
        return map;
    }


    @UserSessionRequired
    @GetMapping("/boxBook")
    @ApiOperation(value = "箱库存-库存流水查询（分页）", notes = "库存流水查询（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyIds", value = "客户 多个,隔开"),
            @ApiImplicitParam(name = "orderTypes", value = "单据类型 多个,隔开"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "noteNo", value = "关联单据号"),
            @ApiImplicitParam(name = "cellNo", value = "库位编码"),
            @ApiImplicitParam(name = "skuAccurate", value = "箱类型号"),
            @ApiImplicitParam(name = "customizeBarcode", value = "自定义箱条码"),
            @ApiImplicitParam(name = "innerBatchNo", value = "批次号"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = FlowingPageDto.class))
    public PageResult boxBook(HttpServletRequest request){

        Map<String, Object> map = this.getBoxMap(request);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }

    public Map<String, Object> getBoxMap(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String ids = request.getParameter("ids");
        if (ids != null) {
            map.put("ids", Arrays.asList(ids.split(",")));
        }
        map.put("tenantId", getTenantId());
        map.put("flowingType", "box");
        map.put("userId", getCurrentUserId());
        map.put("source", "wms");
        map.put("warehouseIds", Collections.singletonList(getWarehouseId()));
        String companyIds = request.getParameter("companyIds");
        if (StringUtils.isNotBlank(companyIds)) {
            map.put("companyIds", Arrays.asList(companyIds.split(",")));
        }
        String orderTypes = request.getParameter("orderTypes");
        if (StringUtils.isNotBlank(orderTypes)) {
            map.put("orderTypes", Arrays.asList(orderTypes.split(",")));
        }
        String beginTime = request.getParameter("beginTime");
        String endTime = request.getParameter("endTime");
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        String noteNo = request.getParameter("noteNo");
        String cellNo = request.getParameter("cellNo");
        String skuAccurate = request.getParameter("skuAccurate");
        String customizeBarcode = request.getParameter("customizeBarcode");
        String innerBatchNo = request.getParameter("innerBatchNo");

        map.put("noteNo", noteNo);
        map.put("cellNo", cellNo);
        map.put("skuAccurate", skuAccurate);
        map.put("customizeBarcode", customizeBarcode);
        map.put("innerBatchNo", innerBatchNo);
        return map;
    }



    @UserSessionRequired
    @GetMapping("/boxExportFlowingWms")
    @ApiOperation(value = "箱库存流水导出", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id 多个,隔开"),
            @ApiImplicitParam(name = "companyIds", value = "客户 多个,隔开"),
            @ApiImplicitParam(name = "orderTypes", value = "单据类型 多个,隔开"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "noteNo", value = "关联单据号"),
            @ApiImplicitParam(name = "cellNo", value = "库位编码"),
            @ApiImplicitParam(name = "skuAccurate", value = "箱类型号"),
            @ApiImplicitParam(name = "customizeBarcode", value = "自定义箱条码"),
            @ApiImplicitParam(name = "innerBatchNo", value = "批次号"),
    })
    public void boxExportFlowingOms(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = this.getBoxMap(request);
        service.boxExportFlowingWms(map, response);
    }


    @UserSessionRequired
    @GetMapping("/productExportFlowingWms")
    @ApiOperation(value = "产品库存导出", notes = "产品库存导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id 多个,隔开"),
            @ApiImplicitParam(name = "companyIds", value = "客户 多个,隔开"),
            @ApiImplicitParam(name = "stockAttribute", value = "库存属性 0正品 1次品"),
            @ApiImplicitParam(name = "orderTypes", value = "单据类型 多个,隔开"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "noteNo", value = "关联单据号"),
            @ApiImplicitParam(name = "cellNo", value = "库位编码"),
            @ApiImplicitParam(name = "innerBatchNo", value = "批次号"),
            @ApiImplicitParam(name = "barcode", value = "barcode-模糊搜索"),
            @ApiImplicitParam(name = "barcodeAccurate", value = "barcode-精准搜索"),
            @ApiImplicitParam(name = "productName", value = "产品名称-模糊搜索"),
            @ApiImplicitParam(name = "skuAccurate", value = "sku-精准搜索"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = FlowingPageDto.class))
    public void productExportFlowingOms(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = this.getProductMap(request);
        service.productExportFlowingWms(map, response);
    }


}

